
#ifndef RENDERBLOCK_H_
#define RENDERBLOCK_H_

#include "TextureManager.h"

const float BLOCKSIZE = 0.25f;
const float MAX_LIGHT = 10.0f;

void draw2DQuad(OamState oam, int id, int x, int y, int texX, int texY) {
	for(int i = 0; i < 64; i++) {
		for(int j = 0; j < 2; j++) {
			gfx[id][i+j*64] = pcx.image.data16[i+j*1024+((texX)*64+(texY)*1024*2)];
		}
	}
	oamSet(&oam,id,x,y,0,0,SpriteSize_16x16,SpriteColorFormat_256Color,gfx[id],0,false,false,false,false,false);
	oamFreeGfx(&oam, gfx[id]);
}

void drawCube(float x, float y, float z, bool* sides, Block* block, int lightlevel) {
	int uv;
	float ff = 1.0f/16.0f;

	x *= BLOCKSIZE;
	y *= BLOCKSIZE;
	z *= BLOCKSIZE;

	float r = 1.0f*(lightlevel/MAX_LIGHT);
	float g = 1.0f*(lightlevel/MAX_LIGHT);
	float b = 1.0f*(lightlevel/MAX_LIGHT);
	glColor3f(r, g, b);

	//glBindTexture(GL_TEXTURE_2D, TERRAIN_TEXTURE[0]);

	glBegin(GL_QUADS);
	if(sides[0] == true) {
		// Back Face
		uv = block->getTextureFrom(0);
		glTexCoord2f((uv%16)*ff, (uv/16)*ff+ff); glVertex3f(x, y, z);
		glTexCoord2f((uv%16)*ff+ff, (uv/16)*ff+ff); glVertex3f(x+BLOCKSIZE, y, z);
		glTexCoord2f((uv%16)*ff+ff, (uv/16)*ff); glVertex3f(x+BLOCKSIZE, y+BLOCKSIZE, z);
		glTexCoord2f((uv%16)*ff, (uv/16)*ff); glVertex3f(x, y+BLOCKSIZE, z);
	}
	if(sides[1] == true) {
		// Front Face
		uv = block->getTextureFrom(1);
		glTexCoord2f((uv%16)*ff+ff, (uv/16)*ff+ff); glVertex3f(x, y, z+BLOCKSIZE);
		glTexCoord2f((uv%16)*ff+ff, (uv/16)*ff); glVertex3f(x, y+BLOCKSIZE, z+BLOCKSIZE);
		glTexCoord2f((uv%16)*ff, (uv/16)*ff); glVertex3f(x+BLOCKSIZE, y+BLOCKSIZE, z+BLOCKSIZE);
		glTexCoord2f((uv%16)*ff, (uv/16)*ff+ff); glVertex3f(x+BLOCKSIZE, y, z+BLOCKSIZE);
	}
	if(sides[2] == true) {
		// Bottom Face
		uv = block->getTextureFrom(2);
		glTexCoord2f((uv%16)*ff+ff, (uv/16)*ff+ff); glVertex3f(x, y, z);
		glTexCoord2f((uv%16)*ff+ff, (uv/16)*ff); glVertex3f(x, y, z+BLOCKSIZE);
		glTexCoord2f((uv%16)*ff, (uv/16)*ff); glVertex3f(x+BLOCKSIZE, y, z+BLOCKSIZE);
		glTexCoord2f((uv%16)*ff, (uv/16)*ff+ff); glVertex3f(x+BLOCKSIZE, y, z);
	}
	if(sides[3] == true) {
		// Top Face
		uv = block->getTextureFrom(3);
		glTexCoord2f((uv%16)*ff+ff, (uv/16)*ff); glVertex3f(x, y+BLOCKSIZE, z);
		glTexCoord2f((uv%16)*ff, (uv/16)*ff); glVertex3f(x+BLOCKSIZE, y+BLOCKSIZE, z);
		glTexCoord2f((uv%16)*ff, (uv/16)*ff+ff); glVertex3f(x+BLOCKSIZE, y+BLOCKSIZE, z+BLOCKSIZE);
		glTexCoord2f((uv%16)*ff+ff, (uv/16)*ff+ff); glVertex3f(x, y+BLOCKSIZE, z+BLOCKSIZE);
	}
	if(sides[4] == true) {
		// Left face
		uv = block->getTextureFrom(4);
		glTexCoord2f((uv%16)*ff+ff, (uv/16)*ff+ff); glVertex3f(x, y, z);
		glTexCoord2f((uv%16)*ff+ff, (uv/16)*ff); glVertex3f(x, y+BLOCKSIZE, z);
		glTexCoord2f((uv%16)*ff, (uv/16)*ff); glVertex3f(x, y+BLOCKSIZE, z+BLOCKSIZE);
		glTexCoord2f((uv%16)*ff, (uv/16)*ff+ff); glVertex3f(x, y, z+BLOCKSIZE);
	}
	if(sides[5] == true) {
		// Right Face
		uv = block->getTextureFrom(5);
		glTexCoord2f((uv%16)*ff, (uv/16)*ff+ff); glVertex3f(x+BLOCKSIZE, y, z);
		glTexCoord2f((uv%16)*ff+ff, (uv/16)*ff+ff); glVertex3f(x+BLOCKSIZE, y, z+BLOCKSIZE);
		glTexCoord2f((uv%16)*ff+ff, (uv/16)*ff); glVertex3f(x+BLOCKSIZE, y+BLOCKSIZE, z+BLOCKSIZE);
		glTexCoord2f((uv%16)*ff, (uv/16)*ff); glVertex3f(x+BLOCKSIZE, y+BLOCKSIZE, z);
	}
	glEnd();

	//glBindTexture(GL_TEXTURE_2D, 0);
}

#endif /* RENDERBLOCK_H_ */
